<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="org.apache.shiro.web.filter.authc.FormAuthenticationFilter"%>
<%@page import="com.liusany.myshop.service.security.FormAuthenticationWithLockFilter"%>
<%@ page import="org.apache.shiro.authc.ExcessiveAttemptsException"%>
<%@ page import="com.liusany.myshop.service.security.IncorrectCaptchaException"%>
<%@ page import="org.apache.shiro.authc.IncorrectCredentialsException"%>
<%@ page import="java.util.concurrent.atomic.AtomicLong"%>
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - Powered By SHOP++</title>
<meta name="Author" content="SHOP++ Team" />
<meta name="Copyright" content="SHOP++" />
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link href="${ctx}/template/admin/css/base.css" rel="stylesheet" type="text/css" />
<link href="${ctx}/template/admin/css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="${ctx}/template/common/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/template/admin/js/base.js"></script>
<script type="text/javascript" src="${ctx}/template/admin/js/admin.js"></script>
<script type="text/javascript">
$().ready( function() {

	var $loginForm = $("#loginForm");
	var $username = $("#username");
	var $password = $("#password");
	var $captcha = $("#captcha");
	var $captchaImage = $("#captchaImage");
	var $isRememberUsername = $("#isRememberUsername");

	// 判断"记住用户名"功能是否默认选中,并自动填充登录用户名
	if(getCookie("adminUsername") != null) {
		$isRememberUsername.attr("checked", true);
		$username.val(getCookie("adminUsername"));
		$password.focus();
	} else {
		$isRememberUsername.attr("checked", false);
		$username.focus();
	}

	// 提交表单验证,记住登录用户名
	$loginForm.submit( function() {
		if ($username.val() == "") {
			$.dialog({type: "warn", content: "请输入您的用户名!", modal: true, autoCloseTime: 3000});
			return false;
		}
		if ($password.val() == "") {
			$.dialog({type: "warn", content: "请输入您的密码!", modal: true, autoCloseTime: 3000});
			return false;
		}
		/*
		if ($captcha.val() == "") {
			$.dialog({type: "warn", content: "请输入您的验证码!", modal: true, autoCloseTime: 3000});
			return false;
		}*/
		
		if ($isRememberUsername.attr("checked") == true) {
			var expires = new Date();
			expires.setTime(expires.getTime() + 1000 * 60 * 60 * 24 * 7);
			setCookie("adminUsername", $username.val(), expires);
		} else {
			removeCookie("adminUsername");
		}
		
	});

	// 刷新验证码
	$captchaImage.click( function() {
		var timestamp = (new Date()).valueOf();
		var imageSrc = $captchaImage.attr("src");
		if(imageSrc.indexOf("?") >= 0) {
			imageSrc = imageSrc.substring(0, imageSrc.indexOf("?"));
		}
		imageSrc = imageSrc + "?timestamp=" + timestamp;
		$captchaImage.attr("src", imageSrc);
	});
	
	
	
	<%
	String error = (String) request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
	if(error != null){
	    if (error.equals(ExcessiveAttemptsException.class.getName())) {
	%>
		$.dialog({type: "error", content: "账户已被锁，请联系管理员.", modal: true, autoCloseTime: 3000});
	<%
		}else if(error.equals(IncorrectCredentialsException.class.getName())) {
	%>
	$.dialog({type: "error", content: "密码错误，还有<%=FormAuthenticationWithLockFilter.accountLockMap.get(request.getParameter(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM)).get() %>次重试机会", modal: true, autoCloseTime: 3000});
	<%
		}else if(error.equals(IncorrectCaptchaException.class.getName())){
	%>		
			$.dialog({type: "error", content: "验证码错误.", modal: true, autoCloseTime: 3000});
    <%	}
	else{
	%>
		$.dialog({type: "error", content: "登录失败，请重试.", modal: true, autoCloseTime: 3000});
	<%
		}
	}
	%>
	
});
</script>
</head>
<body class="login">
	<script type="text/javascript">

		// 登录页面若在框架内，则跳出框架
		if (self != top) {
			top.location = self.location;
		};

	</script>
	<div class="blank"></div>
	<div class="blank"></div>
	<div class="blank">
	</div>
	<div class="body">
	     <form:form id="loginForm" modelAttribute="loginCommand" action="${ctx}/security/login" method="post">
	     
            <table class="loginTable">
            	<tr>
            		<td rowspan="3">
            			<img src="${ctx}/template/admin/images/login_logo.jpg" alt="爱妆 管理中心" />
            		</td>
                    <th>
                    	用户名:
                    </th>
					<td>
					    <form:input path="username" cssClass="formText" value="admin"/>
                    </td>
                </tr>
                <tr>
					<th>
						密&nbsp;&nbsp;&nbsp;码:
					</th>
                    <td>
                        <input type="password" id="password" name="password" value="admin" class="formText"/>
                    </td>
                </tr>
                <tr>
                	<th>&nbsp;
                		
                	</th>
                    <td>
                    	<label>
                    		<input type="checkbox" id="isRememberUsername" />&nbsp;记住用户名
                    	</label>
                    </td>
                </tr>
                <tr>
                	<th>&nbsp;
                	</th>
                	<td>&nbsp;</td>
                    <td>
                        <input type="button" class="homeButton" value="" onClick="window.open('${ctx}')" hidefocus /><input type="submit" class="submitButton" value="登 录" hidefocus />
                    </td>
                </tr>
            </table>
            <div class="powered">
            	COPYRIGHT © 2012-2015 http://www.lovezhuang.com ALL RIGHTS RESERVED.
            </div>
        </form:form>
	</div>
</body>
</html>